package com.xcx.eduservice.controller.logindexcontorller;


import com.xcx.commonuntils.JwtUtils;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import com.xcx.eduservice.entity.logentity.UcenterMember;
import com.xcx.eduservice.request.logrequest.LogWxRequest;
import com.xcx.eduservice.request.logrequest.RegisterRequest;
import com.xcx.eduservice.request.logrequest.UcenterRequest;
import com.xcx.eduservice.response.loginindexresponse.LoginUserInfoResponse;
import com.xcx.eduservice.service.UcenterMemberService;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.Objects;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-10
 */
@Api(tags = "登录管理api")
@RestController
@RequestMapping("/ucentermember")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
//@CrossOrigin//解决跨域问题
public class UcenterMemberController {
    final private UcenterMemberService memberService;


    @ApiOperation(value = "用户登陆接口")
    @PostMapping("/login")
    public ReturnMessage<LoginUserInfoResponse> loginUser(@RequestBody UcenterRequest member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        return memberService.login(member);
    }




    @ApiOperation(value = "微信登陆接口")
    @PostMapping("/loginWx")
    public ReturnMessage<LoginUserInfoResponse> loginWx(@RequestBody @Valid LogWxRequest member,BindingResult br) {
        log.info("Request:{}", member);
        if (member == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return memberService.loginWx(member);
    }



    //注册
    @PostMapping("/register")
    @ApiOperation(value = "注册接口")
    public ReturnMessage<Object> registerUser(@RequestBody @Valid RegisterRequest registerVo, BindingResult br) {

        log.info("Request:{}", registerVo);
        if (registerVo == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return memberService.register(registerVo);
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    @ApiOperation(value = "根据token获取用户信息(token放在请求头中)")
    public ReturnMessage<Object> getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return ReturnMessageUtils.success(member);
    }







    //根据用户id获取用户信息
//    @PostMapping("getUserInfoById")
//    public UcenterMemberOrder getUserInfoOrder(@RequestBody RegisterRequest registerVo) {
//        UcenterMember member = memberService.getById(id);
//        //把member对象里面值复制给UcenterMemberOrder对象
//        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
//        BeanUtils.copyProperties(member, ucenterMemberOrder);
//        return ucenterMemberOrder;
//    }

//    //查询某一天注册人数
//    @GetMapping("countRegister/{day}")
//    public ReturnMessage<Object> countRegister(@PathVariable String day) {
//
//        return memberService.countRegisterDay(day);
//    }
//





}

